package ModelLayer;
import java.util.ArrayList;

/**
 * The Addressbook class contains a ArrayList of persons. 
 * 
 * @author (group 1(A)) 
 * @version (11/4-2011)
 */
public class AddressBook
{
    private ArrayList<Person> persons;

    /**
     * Constructor for objects of class Addressbook
     */
    public AddressBook()
    {
        persons = new ArrayList<Person>();
    }
    
    /**
     * Add a person to Addressbook
     * @param take a parameter of type person
     */
    public void addPerson(Person newperson)
    {
        persons.add(newperson);
    }
    
    /**
     * Delete person from the addressbook
     * @param take an objekt of the type person
     */
    public void deletePerson(int id)
    {
        Person p = findPerson(id);
        persons.remove(p);
    }
    
    /**
     * Find a person in the addressbook
     * @param take a parameter type String
     * @return returns a person
     */
    public Person findPerson(int id)
    {
        boolean found = false;
        int index = 0;
        Person p = null;
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getId() == id)
            {
                found = true;
                p = persons.get(index);
            }
            index++;
        }
        if(!found)
        {
            System.out.println("Person not found!!");
        }
        
        return p;
    }
    
    public void updatePerson(int id, String name, String address, int zipCode, String city, int phoneNumber)
    {
        Person p = findPerson(id);
        p.setName(name);
        p.setAddress(address);
        p.setZipCode(zipCode);
        p.setCity(city);
        p.setPhoneNumber(phoneNumber);    
    }
    
    public void listAllPersons()
    {
        System.out.println("Id:  Name: ");
        for(Person p : persons)
        {
            System.out.println(p.getId()+"    " + p.getName());
        }
    }
            
}
